package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingPlanDto;
import com.zzyl.dto.NursingProjectPlanDto;
import com.zzyl.entity.NursingPlan;
import com.zzyl.entity.NursingProjectPlan;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.mapper.NursingProjectPlanMapper;
import com.zzyl.service.NursingPlanService;
import com.zzyl.vo.NursingPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.List;

@Transactional(rollbackFor = Exception.class)
@Service
public class NursingPlanServiceImpl implements NursingPlanService {

    @Autowired
    private NursingPlanMapper nursingPlanMapper;

    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;

    /**
     * 查询所有护理计划
     * @return
     */
    @Override
    public List<NursingPlanVo> showAllNursingPlan() {
    return nursingPlanMapper.selectAllNursingPlan();
    }

    /**
     * 分页查询护理计划
     * @param name
     * @param pageNum
     * @param pageSize
     * @param status
     * @return
     */
    @Override
    public PageResponse<NursingPlanVo> queryPage(String name, Integer pageNum, Integer pageSize, Integer status) {
        PageHelper.startPage(pageNum,pageSize);
        Page<NursingPlanVo> page = nursingPlanMapper.queryPage(name,status);
        return PageResponse.of(page,NursingPlanVo.class);
    }

    /**
     * 根据id查询护理计划
     * @param id
     * @return
     */
    @Override
    public NursingPlanVo showNursingPlanById(Integer id) {
        return nursingPlanMapper.selectNursingPlanById(id);
    }

    /**
     * 新增护理计划
     * @param nursingPlanDto
     */
    @Override
    public void addNursingPlan(NursingPlanDto nursingPlanDto) {
        //把护理计划Dto转为Entity
        NursingPlan nursingPlan = BeanUtil.toBean(nursingPlanDto, NursingPlan.class);
        nursingPlan.setStatus(1);
        nursingPlanMapper.insertNursingPlan(nursingPlan);

        //获取护理计划项目
        List<NursingProjectPlanDto> nursingProjectPlanVoList = nursingPlanDto.getProjectPlans();
        ArrayList<NursingProjectPlan> projectPlanList = new ArrayList<>();
        nursingProjectPlanVoList.forEach(nppl->{
            NursingProjectPlan nursingProjectPlan = BeanUtil.toBean(nppl, NursingProjectPlan.class);
            nursingProjectPlan.setPlanId(nursingPlan.getId());
            projectPlanList.add(nursingProjectPlan);
        });
        nursingProjectPlanMapper.insertProjectPlan(projectPlanList);
    }

    /**
     * 修改护理计划
     * @param nursingPlanDto
     */
    @Override
    public void updateNursingPlan(NursingPlanDto nursingPlanDto) {
        NursingPlan nursingPlan = BeanUtil.toBean(nursingPlanDto, NursingPlan.class);
        nursingPlanMapper.updateNursingPlan(nursingPlan);

        //获取护理项目
        List<NursingProjectPlanDto> projectPlans = nursingPlanDto.getProjectPlans();
        ArrayList<NursingProjectPlan> projectPlanList = new ArrayList<>();
        projectPlans.forEach(projectPlan ->{
            projectPlan.setPlanId(nursingPlanDto.getId());
            NursingProjectPlan nursingProjectPlan = BeanUtil.toBean(projectPlan, NursingProjectPlan.class);
            projectPlanList.add(nursingProjectPlan);
        });

        //删除该计划包含的护理项目
        nursingProjectPlanMapper.deleteProjectByPlanId(nursingPlanDto.getId());
        //重新插入护理项目
        nursingProjectPlanMapper.insertProjectPlan(projectPlanList);
    }

    /**
     * 根据id删除护理计划
     * @param id
     */
    @Override
    public void deleteNursingPlan(Integer id) {
        nursingPlanMapper.deleteNursingPlanById(id);
        nursingProjectPlanMapper.deleteProjectByPlanId(Long.valueOf(id));
    }

    /**
     * 启用禁用护理计划
     * @param id
     * @param status
     */
    @Override
    public void updateStatusById(Integer id, Integer status) {
        nursingPlanMapper.updateStatusById(id,status);
    }
}
